package face.pay.web.enterprise.controller.applyment.alipay;

import face.pay.common.enterprise.service.IAlipayMachInfoService;
import face.pay.dao.AlipayMachInfoDao;
import face.pay.entity.AlipayMachInfo;
import face.pay.entity.UserInfo;
import face.pay.security.annotation.CurrentUser;
import face.pay.util.ResultMessage;
import face.pay.util.page.PageData;
import face.pay.util.page.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/alimchinfo")
public class AliMchInfoController {


    @Autowired
    private IAlipayMachInfoService iAlipayMachInfoService;

    @Autowired
    private AlipayMachInfoDao alipayMachInfoDao;

    @RequestMapping("/index")
    public String manager(@CurrentUser UserInfo loginUser, Integer mchUserId, Model model) {

        Integer mcId = mchUserId == null ? loginUser.getId() : mchUserId;

        AlipayMachInfo alipayMachInfo = this.iAlipayMachInfoService.queryAlipayMachInfo(loginUser, mcId);
        model.addAttribute("alipayMachInfo", alipayMachInfo);

        return "applyment/alipay_mch_info";
    }


    /**
     * 文件上传
     *
     * @param file
     * @param loginUser
     * @return
     */
    @RequestMapping("/fileUpload")
    @ResponseBody
    public ResultMessage fielUpload(@RequestParam("file") MultipartFile file, String keyName, Integer userId, @CurrentUser UserInfo loginUser) throws Exception {
        byte[] fileBytes = file.getBytes();
        String originalFilename = file.getOriginalFilename();
        return this.iAlipayMachInfoService.fielUpload(fileBytes, originalFilename, keyName, userId,loginUser);
    }


    /**
     * 商户支付宝进件资料编辑
     *
     * @param
     * @param loginUser
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit")
    public ResultMessage subinfo(AlipayMachInfo alipayMachInfo, @CurrentUser UserInfo loginUser) {
        return this.iAlipayMachInfoService.edit(alipayMachInfo, loginUser);
    }


    /**
     * oem管理进件页面跳转
     *
     * @return
     */
    @RequestMapping("/oem_ManageList")
    public String oemManageIndex() {
        return "applyment/aliPay_mch_info_ManageList";
    }


    /**
     * 分页查询用户进件申请列表
     *
     * @param loginUser
     * @return
     */
    @RequestMapping("/queryData")
    @ResponseBody
    public PageData queryData(@CurrentUser UserInfo loginUser, AlipayMachInfo alipayMachInfo, PageParam pageParam) {
        return this.iAlipayMachInfoService.queryData(pageParam, alipayMachInfo, loginUser);
    }


    /**
     * oem处理
     *
     * @param loginUser
     * @param model
     * @return
     */
    @RequestMapping("/deal")
    public String index(@CurrentUser UserInfo loginUser, Integer id, Model model) {

        AlipayMachInfo record = new AlipayMachInfo();
        record.setId(id);
        AlipayMachInfo result = this.alipayMachInfoDao.queryAlipayMachInfo(record);
        if (result == null) {
            AlipayMachInfo info = new AlipayMachInfo();
            model.addAttribute("alipayMachInfo", info);
        } else {
            model.addAttribute("alipayMachInfo", result);
        }

        return "applyment/alipay_mch_info_deal";
    }


    /**
     * 商户支付宝进件资料编辑
     *
     * @param
     * @param loginUser
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/oem_edit")
    public ResultMessage subInfobyOem(AlipayMachInfo alipayMachInfo, @CurrentUser UserInfo loginUser) {
        return this.iAlipayMachInfoService.oemEdit(alipayMachInfo, loginUser);
    }
}
